package com.jgsu.farm.controller;

import com.github.pagehelper.PageInfo;
import com.jgsu.farm.entity.StressCategory;
import com.jgsu.farm.exception.DataAddException;
import com.jgsu.farm.exception.DataNotExistException;
import com.jgsu.farm.service.StressCategoryService;
import com.jgsu.farm.utils.CommonResult;
import com.jgsu.farm.utils.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @program: farm-system
 * @description: 胁迫类型
 * @author: Mr.zuo
 * @create: 2020-01-15 12:37
 **/
@Slf4j
@Api(value = "farm/sysuser/stresscategory", tags = "胁迫类型管理模块")
@RestController
@RequestMapping("farm/sysuser/stresscategory")
public class StressCategoryController {

    @Autowired
    StressCategoryService stressCategoryService;


    @ApiOperation(value = "胁迫类型添加功能")
    @PostMapping(value = "/add", produces = "application/json")
    public CommonResult stressCategoryAdd(StressCategory stresscategory) {
        try {
            stressCategoryService.add(stresscategory);
            log.info("{}胁迫类型添加成功", stresscategory.getName());
            return CommonResult.success(ResultCode.SUCCESS);
        } catch (DataAddException e) {
            log.info("{}胁迫类型添加失败", stresscategory.getName());
            throw new DataAddException(e.getCode(), e.getMessage());
        }
    }

    @ApiOperation(value = "分页查询胁迫类型信息", notes = "分页查询胁迫类型信息")
    @GetMapping(produces = "application/json")
    public CommonResult<PageInfo<StressCategory>> stressCategoryList(@RequestParam(value = "page", required = false, defaultValue = "1") int page,
                                                                     @RequestParam(value = "pagesize", required = false, defaultValue = "10") int pagesize) {
        try {
            PageInfo<StressCategory> stressCategories = stressCategoryService.getStressCategories(page, pagesize);
            log.info("查询第{}页所有胁迫类型", page);
            return CommonResult.success(stressCategories);
        } catch (DataNotExistException e) {
            log.error("第{}页胁迫类型查询失败", page);
            throw new DataNotExistException(e.getCode(), e.getMessage());
        }
    }

    @ApiOperation(value = "胁迫类型删除功能")
    @DeleteMapping(value = "/delete", produces = "application/json")
    public CommonResult stressCategoryDelete(Integer stressid) {
        try {
            stressCategoryService.delete(stressid);
            log.info("删除id为:{}的胁迫类型信息成功", stressid);
            return CommonResult.success(ResultCode.SUCCESS);
        } catch (DataNotExistException e) {
            log.info("删除id为:{}的胁迫类型信息失败", stressid);
            throw new DataNotExistException(e.getCode(), e.getMessage());
        }
    }

    @ApiOperation(value = "胁迫类型更新功能")
    @PutMapping(value = "/update", produces = "application/json")
    public CommonResult stressCategoryUpdate(StressCategory stressCategory) {
        try {
            stressCategoryService.update(stressCategory);
            log.info("更新:{}的胁迫类型信息成功", stressCategory);
            return CommonResult.success(stressCategory);
        } catch (DataNotExistException e) {
            log.info("更新:{}的胁迫类型信息失败", stressCategory);
            throw new DataNotExistException(e.getCode(), e.getMessage());
        }
    }
}
